package servlet;

import dao.GoodsDao;
import dao.UserDao;
import entity.Goods;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doGet(request, response);
    }
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String loginName = request.getParameter("loginname");
		String pwd = request.getParameter("pwd");
		String yzm = request.getParameter("yanzhengma");
		if (!yzm.equals("53t4")){
			System.out.println("验证码错误");
			request.getSession().setAttribute("errorMsg", "验证码错误");
			response.sendRedirect("frontend/index.jsp");
			return;
		}
		UserDao dao = new UserDao();
		User user = dao.login(loginName, pwd);
		if (user == null) {
			//用户名或密码错误,登录失败
			request.getSession().setAttribute("errorMsg", "用户名或密码错误");
			response.sendRedirect("frontend/index.jsp");
		}else {
			request.getSession().setAttribute("user", user);
			response.sendRedirect("frontend/index.jsp");
		}
	}
}
